package com.sfpay.coreplatform.member.service.proxy.account.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sfpay.coreplatform.account.service.IAccountMgrService;
import com.sfpay.coreplatform.account.service.IAccountPasswordMgrService;
import com.sfpay.coreplatform.account.service.exception.AccountServiceException;
import com.sfpay.coreplatform.account.valueobject.dto.OpenAcc;
import com.sfpay.coreplatform.account.valueobject.dto.OpenAccReturn;
import com.sfpay.coreplatform.account.valueobject.dto.ResetAccountPsw;
import com.sfpay.coreplatform.account.valueobject.dto.UpdateAccountPsw;
import com.sfpay.coreplatform.account.valueobject.tmo.AccountVO;
import com.sfpay.coreplatform.member.common.MemberResponseCode;
import com.sfpay.coreplatform.member.common.constant.Constants;
import com.sfpay.coreplatform.member.service.exception.MemberServiceException;
import com.sfpay.coreplatform.member.service.proxy.account.IAcctProxyManageService;
import com.sfpay.framework.common.util.StringUtils;

/**
 * 
 * 
 * 类说明：调用外部：账户模块接口
 * 
 * 
 * <p>
 * 详细描述：
 * 
 * 
 * @author 313717
 * 
 *         CreateDate: 2012-5-4
 */
@Service
public class AcctProxyManageServiceImpl implements IAcctProxyManageService {
	@Resource
	private IAccountPasswordMgrService accountPasswordService;

	@Resource
	private IAccountMgrService accountMgrService;


	private static Logger logger = LoggerFactory
			.getLogger(AcctProxyManageServiceImpl.class);

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void updateAccountPsw(UpdateAccountPsw updateAccountPsw) throws MemberServiceException 
	{

		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:updateAccountPsw,start invoke account,the parameters 's info"
					+ "{updateAccountPswList" + updateAccountPsw.toString() + "}");
		}

		// 调用账户
		try 
		{
			accountPasswordService.updateAccountPsw(updateAccountPsw);
		}
		catch (AccountServiceException e) 
		{
			logger.error("invoke account updateAccountPsw interface ServiceException:",e);
			throw new MemberServiceException(MemberResponseCode.ACCOUNT_SERVICE_EXCEPTION, "account service error" + e.getMessage());
		} 
		catch (Exception e) 
		{
			logger.error("invoke account updateAccountPsw interface exception:", e);
			throw new MemberServiceException(MemberResponseCode.HESSIAN_INVOKE_EXCEPTION,
					"invoke account updateAccountPsw interface exception" ,e);
		}

		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:updateAccountPsw,end invoke account,the parameters 's info"
					+ "{updateAccountPsw" + updateAccountPsw.toString() + "}");
		}

	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void resetAccountPsw(ResetAccountPsw resetAccountPsw) throws MemberServiceException 
	{

		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:resetAccountPsw,start invoke account,the parameters 's info"
					+ "{resetAccountPswList" + resetAccountPsw.toString() + "}");
		}
		try 
		{
			accountPasswordService.resetAccountPsw(resetAccountPsw);
		} 
		catch (AccountServiceException e) 
		{
			logger.error("invoke account resetAccountPsw ServiceException:", e);
			throw new MemberServiceException(MemberResponseCode.ACCOUNT_SERVICE_EXCEPTION, "account service error" + e.getMessage());
		}
		catch (Exception e) 
		{
			logger.error("invoke account resetAccountPsw exception:", e);
			throw new MemberServiceException(MemberResponseCode.HESSIAN_INVOKE_EXCEPTION,
					"invoke account resetAccountPsw exception", e);
		}

		if (logger.isDebugEnabled())
		{
			logger.debug("method:resetAccountPsw,end invoke account,the parameters 's info"
					+ "{resetAccountPswList" + resetAccountPsw.toString() + "}");
		}

	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public OpenAccReturn openAccount(OpenAcc openAcc) throws MemberServiceException 
	{
		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:openPersonAcc, person member add account,the parameters 's info"
					+ "{openAcc" + openAcc + "}");
		}

		OpenAccReturn openAccRet;
		try 
		{
			if (StringUtils.isNullOrEmpty(openAcc.getSubjectNo())) 
			{
				
				if(openAcc.getAccountTypeNo() == Constants.ACCOUNT_TYPE_NO_FOR_101)
				{
					openAcc.setSubjectNo(Constants.DEFAULT_SUBJECT_NO_FOR_101);
				}
				else if(openAcc.getAccountTypeNo() == Constants.ACCOUNT_TYPE_NO_FOR_102)
				{
					openAcc.setSubjectNo(Constants.DEFAULT_SUBJECT_NO_FOR_102);
				}
			}
			openAccRet = accountMgrService.openAccount(openAcc);
		} 
		catch (AccountServiceException e) 
		{
			logger.error("invoke account openPersonAcc ServiceException:", e);
			throw new MemberServiceException(MemberResponseCode.ACCOUNT_SERVICE_EXCEPTION, "account service error" + e.getMessage());
		} 
		catch (Exception e) 
		{
			logger.error("invoke account openPersonAcc exception:", e);
			throw new MemberServiceException(MemberResponseCode.HESSIAN_INVOKE_EXCEPTION,
					"invoke account openPersonAcc exception", e);
		}

		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:openPersonAcc, person member add account ,end invoke account,the parameters 's info"
					+ "{openAcc" + openAcc + "}");
		}
		return openAccRet;
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void makeAccountActive(Long accountNo) throws MemberServiceException 
	{
		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:makeAccountActive,start active account ,the parameters 's info"
					+ "{accountNo" + accountNo + "}");
		}
		try 
		{
			accountMgrService.makeAccountActive(accountNo);
		} 
		catch (AccountServiceException e) 
		{
			logger.error("invoke account makeAccountActive ServiceException:", e);
			throw new MemberServiceException(MemberResponseCode.ACCOUNT_SERVICE_EXCEPTION, "account service error" + e.getMessage());
		} 
		catch (Exception e) 
		{
			logger.error("invoke account makeAccountActive exception:", e);
			throw new MemberServiceException(MemberResponseCode.HESSIAN_INVOKE_EXCEPTION,
					"invoke account makeAccountActive exception", e);
		}

		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:makeAccountActive,end active account successful,the parameters 's info"
					+ "{accountNo" + accountNo + "}");
		}
	}

	@Override
	public AccountVO makeAccountForbidOut(Long accountNo) throws MemberServiceException 
	{
		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:makeAccountForbidOut start,the parameters 's info"
					+ "{accountNo" + accountNo + "}");
		}
		AccountVO accountVO = null;
		try 
		{
			accountVO = accountMgrService.makeAccountForbidOut(accountNo);
		} 
		catch (AccountServiceException e) 
		{
			logger.error("invoke account makeAccountForbidOut ServiceException:", e);
			throw new MemberServiceException(MemberResponseCode.ACCOUNT_SERVICE_EXCEPTION, "account service error" + e.getMessage());
		} 
		catch (Exception e) 
		{
			logger.error("invoke account makeAccountForbidOut exception:", e);
			throw new MemberServiceException(MemberResponseCode.HESSIAN_INVOKE_EXCEPTION,
					"invoke account makeAccountForbidOut exception", e);
		}
		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:makeAccountForbidOut end,the parameters 's info"
					+ "{accountNo" + accountNo + "}");
		}
		return accountVO;

	}

	@Override
	public AccountVO makeAccountForbidIn(Long accountNo) throws MemberServiceException 
	{

		AccountVO accountVO = null;
		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:makeAccountForbidIn start,the parameters 's info" + "{accountNo" + accountNo + "}");
		}
		try 
		{
			accountVO = accountMgrService.makeAccountForbidIn(accountNo);
		} 
		catch (AccountServiceException e) 
		{
			logger.error("invoke account makeAccountForbidIn ServiceException:", e);
			throw new MemberServiceException(MemberResponseCode.ACCOUNT_SERVICE_EXCEPTION, "account service error" + e.getMessage());
		}
		catch (Exception e) 
		{
			logger.error("invoke account makeAccountForbidIn exception:", e);
			throw new MemberServiceException(MemberResponseCode.HESSIAN_INVOKE_EXCEPTION,"invoke account makeAccountForbidIn exception", e);
		}

		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:makeAccountForbidIn end,the parameters 's info"
					+ "{accountNo" + accountNo + "}");
		}
		return accountVO;
	}

	@Override
	public void makeAccountNormal(Long accountNo) throws MemberServiceException 
	{
		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:makeAccountNormal start,the parameters 's info" + "{accountNo" + accountNo + "}");
		}
		try 
		{
			accountMgrService.makeAccountNormal(accountNo);
		} 
		catch (AccountServiceException e) 
		{
			logger.error("invoke account makeAccountNormal ServiceException:", e);
			throw new MemberServiceException(MemberResponseCode.ACCOUNT_SERVICE_EXCEPTION, "account service error" + e.getMessage());
		} 
		catch (Exception e) 
		{
			logger.error("invoke account makeAccountNormal exception:", e);
			throw new MemberServiceException(MemberResponseCode.HESSIAN_INVOKE_EXCEPTION,"invoke account makeAccountNormal exception", e);
		}

		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:makeAccountNormal end,the parameters 's info"
					+ "{accountNo" + accountNo + "}");
		}

	}

	@Override
	public void makeAccountCancel(Long accountNo) throws MemberServiceException 
	{
		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:makeAccountCancel start,the parameters 's info" + "{accountNo" + accountNo + "}");
		}
		try 
		{
			accountMgrService.makeAccountCancel(accountNo);
		} 
		catch (AccountServiceException e) 
		{
			logger.error(
					"invoke account makeAccountCancel ServiceException:", e);
			throw new MemberServiceException(MemberResponseCode.ACCOUNT_SERVICE_EXCEPTION, "account service error" + e.getMessage());
		} 
		catch (Exception e)
		{
			logger.error("invoke account makeAccountCancel exception:", e);
			throw new MemberServiceException(MemberResponseCode.HESSIAN_INVOKE_EXCEPTION,"invoke account makeAccountCancel exception", e);
		}

		if (logger.isDebugEnabled()) 
		{
			logger.debug("method:makeAccountCancel end,the parameters 's info"
					+ "{accountNo" + accountNo + "}");
		}

	}

}